const app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    disabled: false,
    columns: ['父母', '亲戚', '朋友'],
    show: false,
    realition:"",
  },

  onLoad: function (options) {
    console.log(options.allID)
    this.setData({
      allID:options.allID,
    })
  },
  showPopup() {
    this.setData({ show: true });
  },
  onCancel(){
    this.setData({ show: false });
  },
  onConfirm(event) {
    console.log(event)
    this.setData({
      realition:event.detail.value,
      show: false
    })
  },
  onShow: function (e) {
    //获取时间戳
    this.time = new Date();
    //时间戳转换
    var date = util.formatTime(this.time);
    var timeTamp = Date.parse(date)/ 1000;
    this.setData({ time: timeTamp })
  },
  bindSave:function(e){
    var names=e.detail.value.names
    var phones=e.detail.value.phones
    var messages=e.detail.value.messages
    var realition=this.data.realition
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    // console.log(this.data.studentID)
    if (names == null ) {
      wx.showToast({
        title: '请输入姓名！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if(phones == null ) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } else if(!myreg.test(phones)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if(realition == '' ) {
      wx.showToast({
        title: '请选择关系！',
        icon: 'none',
        duration: 2000,
      })
      return false
    }else if(messages == null ) {
      wx.showToast({
        title: '请输入来访事由！',
        icon: 'none',
        duration: 2000,
      })
      return false
    } 
    wx.cloud.callFunction({
      name:"get-pro-MessageChannel",
      data:{
        phone: phones,
      }
    }).then(res=>{
      // console.log(res.result.length)
      if(res.result.length == 0){
        wx.cloud.callFunction({
              name: "student-pro",
              data:{
                names: e.detail.value.names,
                phones: phones,
                messages:messages,
                realition:realition,
                time:this.data.time,
                allID:this.data.allID
              }
            }).then(res=>{
              console.log(res)
              wx.showToast({
                title: '授权成功',
                icon: 'loading',
                mask:true,
                duration: 2000,
                success:function(){
                  setTimeout(function () {
                    //要延时执行的代码
                    wx.navigateBack({})
                  }, 2000) //延迟时间
                }
              })
            })
      }else{
        wx.showToast({
        title: '手机已经申请',
        icon: 'none',
        duration: 2000,
        })
      }
    })
   
  }
})